<?php
/**
 * 后台用户管理控制器
 */

namespace Apps\Acp\Controllers;

use Apps\Acp\Models\Campaign;
use Apps\Acp\Models\RebateGoods;
use Apps\Common\Constant\BusinessConstant;
use Apps\Common\Controller\AcpBaseController;
use Apps\Fanli\Services\CampaignService;
use Deyi\Functions\PageFunction;
use Deyi\Functions\UtilFunction;

/**
 * Class ManagerController
 * @package Apps\Acp\Controllers
 */
class CampaignController extends AcpBaseController
{
    public function indexAction()
    {
        $data = Campaign::getInstance()->getOneByField('id', 1);
        
        $this->view->setVars([
            'data' => $data,
        ]);
    }

    public function saveAction()
    {
        $id = $this->getParamValue('id', 0, 'int');
        $title = $this->getParamValue('title');
        $cover = $this->getParamValue('cover', '');
        $phone = $this->getParamValue('phone', '');
        $price = $this->getParamValue('price', '');
        $detailTitle = $this->getParamValue('detailTitle', '');
        $detailDesc = $this->getParamValue('detailDesc', '');
        $rule = $this->getParamValue('rule');
        $shareTitle = $this->getParamValue('shareTitle', '');
        $shareFriend = $this->getParamValue('shareFriend', '');
        $shareCircle = $this->getParamValue('shareCircle', '');
        $shareImage = $this->getParamValue('shareImage', '');

        $values = [
            'title' => $title,
            'cover' => $cover,
            'phone' => $phone,
            'price' => $price,
            'detailTitle' => $detailTitle,
            'detailDesc' => $detailDesc,
            'rule' => $rule,
            'shareTitle' => $shareTitle,
            'shareFriend' => $shareFriend,
            'shareCircle' => $shareCircle,
            'shareImage' => $shareImage,
            'dateline' => time(),
        ];

        if ($id) {
            Campaign::getInstance()->updateByFields($values, ['id' => $id]);
        } else {
            Campaign::getInstance()->insertByFields($values);
        }

        parent::renderSuccessJson();
    }

    public function deleteAction()
    {
        $id = $this->getParamValue('id', null, 'int');

        $rebateCampaign = Campaign::getInstance()->getOneByField('id', $id);

        if ($rebateCampaign) {
            Campaign::getInstance()->deleteByField('id', $id);
        }

        parent::renderSuccessJson();
    }
}